<!DOCTYPE html>
<html>
<head>
    <title>新增设备闲置</title>
    #parse("sys/header.html")
    <style>
        .tables {
            width: 60%;
            height: 60%;
            float: left;
        }

        .textarea {
            width: 40%;
            height: 60%;
            float: right;
        }
    </style>
</head>
<body>
<div id="rrapp" v-cloak>
    <div style="padding: 20px">
        <i-form :label-width="100">
            <Row>
                <i-col span="22">
                    <Form-item label="原因：">
                        <i-input type="textarea" :rows="4" v-model="devLeaveRecord.reson" placeholder="闲置原因"></i-input>
                    </Form-item>
                </i-col>
            </Row>
            <Row>
                <i-col span="22">
                    <Form-item label="设备列表：">
                        <table id="jqGrid"></table>
                    </Form-item>
                </i-col>
            </Row>
        </i-form>
    </div>
</div>

<script>
    var ids = getQueryString("ids");
    var dates = '';
    $(function () {
        //获取数据
        var id = ids.split(',');
        Ajax.request({
            url: '../deviceinfo/getDevInfoByIds',
            async: false,
            params: JSON.stringify(id),
            type: "POST",
            contentType: "application/json",
            successCallback: function (r) {
                dates = r.infoList;
            }
        });
        // $("#jqGrid").jqGrid('editRow');
        $("#jqGrid").Grid({
            // url: '',
            datatype: "local",
            height: 'auto',
            pager: null,
            multiselect: false,
            // colNames : [ 'devCode', 'devName', 'origVal', 'netVal'],
            colModel: [
                {label: 'id', name: 'id', index: 'ID', key: true, hidden: true},
                {label: '设备编码', name: 'devCode', index: 'DEV_CODE'},
                {label: '设备名称', name: 'devName', index: 'DEV_NAME'},
                {
                    label: '净值', name: 'netVal', index: 'NET_VAL', editable: true, edittype: "text",
                    editrules: {number: true}
                }
            ],
            gridComplete: function () {
                var rowIds = $("#jqGrid").getDataIDs();
                // var arrayData = new Array();
                if (rowIds.length > 0) {
                    for (var i = 0; i < rowIds.length; i++) {
                        $("#jqGrid").editRow(rowIds[i], true);
                    }
                }
            }
        });

        for (var i = 0; i <= dates.length; i++) {
            jQuery("#jqGrid").jqGrid(
                'addRowData', i + 1, dates[i]);
        }
    });


    var vm = new Vue({
        el: '#rrapp',
        data: {
            showList: true,
            devLeaveRecord: {
                id: '',
                devCode: '',
                devName: '',
                origVal: '',
                // netVal: '',
                reson: '',
                reserve3:''
            },
        }

    });

    function getallDatas() {
        var reson = vm.devLeaveRecord.reson;
        var jqData = [];
        jqData = getAllData("#jqGrid");
        var dates = getalldata("#jqGrid", jqData, reson);
        return dates;
    }
    function review() { //审核
        var result = getSelectedRowData("#jqGrid");
        var id =result.id
        // vm.devLeaveRecord = getallDatas;
        // var id =getSelectedRow("#jqGrid")
        if (id == null) {
            return;
        }

        if (result.reserve3 == "已审核") {

            confirm('确定要审核选中的记录？', function () {
                alert('本记录已审核!');

            });
        } else {
            vm.devLeaveRecord = {id: result.id};
            Ajax.request({
                url: '../deviceinfo/review',
                params: id,
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        // reload();
                        window.location.reload();
                    });
                }
            });
        }

    }
    function reload() {
        vm.showList = true;
        var page = $("#jqGrid").jqGrid('getGridParam', 'page');
        $("#jqGrid").jqGrid('setGridParam', {
            // postData: {'storeCode': vm.q.storeCode, 'columnId': vm.columnId,'storeType':vm.q.storeType,'moduleType':vm.q.moduleType},
            page: page
        }).trigger("reloadGrid");
    }

    function getalldata(jgrid, jqDatas, reson) {
        var jdata=[];
        var rowIds = $("#jqGrid").getDataIDs();
        if (rowIds.length > 1) {
            for (var i = 0; i < rowIds.length; i++) {
                var jdata = jqDatas[i];
                var id = rowIds[i];
                // var netVal = $("#" + id + "_netVal").val();
                var origVal = $("#" + id + "_origVal").val();
                // jdata.netVal = netVal;
                jdata.origVal = origVal;
                jdata.reson = reson;
            }
        } else if (rowIds.length = 1) {
            var jdata1 = $(jgrid).jqGrid('getRowData', 1);
            // var netVal = $("#1_netVal").val();
            var origVal = $("#1_origVal").val();
            // jdata1.netVal = netVal;
            jdata1.origVal = origVal;
            jdata1.reson=reson;
            jdata.push(jdata1);
        }
        return jdata;
    }

    function getTableDatas(jgrid) {
        var reason = vm.devLeaveRecord.reson;
        var tDatas = [];
        tDatas = getAllData("#jqGrid");
        if(!tDatas.length){
            tDatas = []
            tDatas.push(getAllData("#jqGrid"))
        }
        var jdatas=[];
        var rowIds = $("#jqGrid").getDataIDs();
        if (rowIds.length && rowIds.length > 0) {
            for (var i = 0; i < rowIds.length; i++) {
                var jdata = tDatas[i];
                var id = rowIds[i];
                jdata.netVal = $("#" + id + "_netVal").val();
                jdata.reson = reason;
                jdatas.push(jdata);
            }
        }
        return jdatas;
    }
</script>
</body>
</html>
